var host = 'https://test.ciyuanjuhe.com';
// var host = 'https://pro.ciyuanjuhe.com';


/**
 * POST请求，
 * URL：接口
 * postData：参数，json类型
 * doSuccess：成功的回调函数
 * doFail：失败的回调函数
 */
function post(url, postData, doSuccess, doFail) {
  var app = getApp();
  wx.request({
    //项目的真正接口，通过字符串拼接方式实现
    url: host + url,
    header: {
      "content-type": "application/json;charset=UTF-8",
      "token": app.globalData.token || wx.getStorageSync('token')
    },
    data: postData,
    method: 'POST',
    success: function (res) {
      if (res.data.code == 200 || res.data.code == 2039 || res.data.code == 2040) {
        doSuccess(res.data);
      } else if (res.data.code == 2002) {
        app.globalData.userInfo = {}
        app.globalData.token = ''
        wx.removeStorage({
          key: 'userInfo'
        })
        wx.removeStorage({
          key: 'token'
        })
        wx.showToast({
          title: '登录过期，请重新登录',
          icon: 'none',
          mask: true,
          duration: 2000
        })
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }, 2000)
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.data.msg,
          showCancel: false
        })
        wx.hideLoading()
      }
    },
    fail: function () {
      doFail();
    },
  })
}

//GET请求，不需传参，直接URL调用，
function get(url, doSuccess, doFail) {
  var app = getApp();
  wx.request({
    url: host + url,
    header: {
      "content-type": "*/*",
      "token": app.globalData.token || wx.getStorageSync('token')
    },
    method: 'GET',
    success: function (res) {
      if (res.data.code == 200 || res.data.code == 4000) {
        doSuccess(res.data);
      } else if (res.data.code == 2002) {
        app.globalData.userInfo = {}
        app.globalData.token = ''
        wx.removeStorage({
          key: 'userInfo'
        })
        wx.removeStorage({
          key: 'token'
        })
        wx.showToast({
          title: '登录过期，请重新登录',
          icon: 'none',
          mask: true,
          duration: 2000
        })
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }, 2000)
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.data.msg,
          showCancel: false
        })
        wx.hideLoading()
      }
    },
    fail: function () {
      doFail();
    },
  })
}

function promiseGet(url, doSuccess, doFail) {
  return new Promise((resolve, reject) => {
    var app = getApp();
    wx.request({
      url: host + url,
      header: {
        "content-type": "*/*",
        "token": app.globalData.token || wx.getStorageSync('token')
      },
      method: 'GET',
      success: function (res) {
        if (res.data.code == 200) {
          resolve(res.data)
        } else if (res.data.code == 2002) {
          app.globalData.userInfo = {}
          app.globalData.token = ''
          wx.removeStorage({
            key: 'userInfo'
          })
          wx.removeStorage({
            key: 'token'
          })
          wx.showToast({
            title: '登录过期，请重新登录',
            icon: 'none',
            mask: true,
            duration: 2000
          })
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }, 2000)
        } else {
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg
          })
          wx.hideLoading()
        }
      },
      fail: function () {
        reject();
      },
    })
  })
}

function promisePost(url, postData, doSuccess, doFail) {
  return new Promise((resolve, reject) => {
    var app = getApp();
    wx.request({
      url: host + url,
      header: {
        "content-type": "application/json;charset=UTF-8",
        "token": app.globalData.token || wx.getStorageSync('token')
      },
      data: postData,
      method: 'POST',
      success: function (res) {
        if (res.data.code == 200) {
          resolve(res.data)
        } else if (res.data.code == 2002) {
          app.globalData.userInfo = {}
          app.globalData.token = ''
          wx.removeStorage({
            key: 'userInfo'
          })
          wx.removeStorage({
            key: 'token'
          })
          wx.showToast({
            title: '登录过期，请重新登录',
            icon: 'none',
            mask: true,
            duration: 2000
          })
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }, 2000)
        } else {
          resolve(res.data)
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg
          })
          wx.hideLoading()
        }
      },
      fail: function () {
        reject();
      },
    })
  })
}

module.exports.host = host;
module.exports.post = post;
module.exports.get = get;
module.exports.promiseGet = promiseGet;
module.exports.promisePost = promisePost;